package com.csdj.t309.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csdj.t309.entity.GymCard;
import com.csdj.t309.mapper.GymCardMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @since 2022-03-05
 */
@Service
public class GymCardService extends ServiceImpl<GymCardMapper, GymCard> implements IGymCardService {

    @Resource
    private GymCardMapper gymCardMapper;

    public Page<GymCard> getListPage(Integer pageNum,Integer size,int type){
        if( pageNum!=null && size!=null ){
            if(pageNum <= 0 ){
                pageNum = 0;
            }else{
                pageNum=(pageNum-1);
            }
        }
        List<GymCard> data = gymCardMapper.findCardListPageByTypeId(pageNum, size, type);
        int total = gymCardMapper.findTotal();
        if( total - (pageNum+1)*size <0 ){
            pageNum = pageNum-1;
        }
        Page<GymCard> page = new Page<>();
        page.setRecords(data);
        page.setTotal(total);
        return page;
    }

    public Page<GymCard> getPage(Page<GymCard> page, QueryWrapper<GymCard> queryWrapper){
        return gymCardMapper.selectPage(page,queryWrapper);
    }

    /**
     * 插入一条记录（选择字段，策略插入）
     *
     * @param entity 实体对象
     */
    @Override
    @Transactional
    public boolean save(GymCard entity) {
        return super.save(entity);
    }

    @Cacheable(cacheNames = "cache")
    public List<GymCard> getListByTypeId(int typeId) {
        return gymCardMapper.findCardListByTypeId(typeId);

    }

    @Override
    @Cacheable(value = "cache")
    public List<GymCard> getList() {
        return gymCardMapper.findList();
    }

    //    @CachePut(value = "emp", key = "targetClass + #p0")
    @CacheEvict( value="cache",allEntries=true)
    public int updateStatus(int status, int id) {
        return gymCardMapper.updateStatus(status, id);
    }


}
